<script>
	export default {
		onLaunch: async function() {
			try {
				const token = uni.getStorageSync('uni_id_token')
				if (token) {
					const uniIdCo = uniCloud.importObject('uni-id-co')
					const res = await uniIdCo.refreshToken()

					if (res.errCode === 0) {
						uni.setStorageSync('uni_id_token', res.newToken.token)
						uni.setStorageSync('uni_id_token_expired', res.newToken.tokenExpired)
					} else {
						uni.removeStorageSync('uni_id_token')
						uni.removeStorageSync('uni_id_token_expired')
					}
				}
			} catch (error) {
				console.error('自动登录失败:', error)
				uni.removeStorageSync('uni_id_token')
				uni.removeStorageSync('uni_id_token_expired')
			}
		},
		onShow: function() {
			// App Show
		},
		onHide: function() {
			// App Hide
		},
		methods: {
			// 检查登录状态
			async checkLoginStatus() {
				try {
					const token = uni.getStorageSync('uni_id_token')
					if (token) {
						const uniIdCo = uniCloud.importObject('uni-id-co')
						const res = await uniIdCo.refreshToken()
						if (res.errCode === 0) {
							uni.setStorageSync('uni_id_token', res.newToken.token)
							uni.setStorageSync('uni_id_token_expired', res.newToken.tokenExpired)
						} else {
							uni.removeStorageSync('uni_id_token')
							uni.removeStorageSync('uni_id_token_expired')
						}
					}
				} catch (error) {
					console.error('登录状态检查失败:', error)
					uni.removeStorageSync('uni_id_token')
					uni.removeStorageSync('uni_id_token_expired')
				}
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	page {
		background-color: #f5f5f5;
		font-size: 28rpx;
		color: #333;
		font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica,
			Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 'Microsoft Yahei',
			sans-serif;
	}
</style>